package com.yjm.test.Spring5.ch5.controlFlow;

import org.springframework.aop.Advisor;
import org.springframework.aop.Pointcut;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.ControlFlowPointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;

public class ControlFlowDemo {

	public static void main(String[] args) {
		ControlFlowDemo ex = new ControlFlowDemo();
		ex.run();
	}

	public void run() {
		TestBean target = new TestBean();
		
		Pointcut pc = new ControlFlowPointcut(ControlFlowDemo.class, "test");
		Advisor advisor = new DefaultPointcutAdvisor(pc, new SimpleBeforeAdvice());
		
		ProxyFactory pf = new ProxyFactory();
		pf.setTarget(target);
		pf.addAdvisor(advisor);
		
		TestBean proxy = (TestBean) pf.getProxy();
		System.out.println("Trying normal invoke");
		proxy.foo();
		System.out.println("\nTrying under ControlFlowDemo.test()");
		test(proxy);
	}
	
	public void test(TestBean bean) {
		bean.foo();
		System.out.println("\t" + bean.toString() + " hashCode:" + bean.hashCode());
		bean.hashCode();
		bean.equals(null);
		try {
			synchronized(bean){
			bean.wait(3000);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		bean.bar();
	}
}
